//package com.lt.test.consumer.controller;
//
//import com.alibaba.dubbo.config.annotation.Reference;
//import com.lt.test.api.CloudlingService;
//import com.lt.test.common.exception.CustomException;
//import com.lt.test.common.util.CustomUtil;
//import com.lt.test.common.util.HttpClientUtil;
//import com.lt.test.common.util.SendMessageUtils;
//import com.lt.test.entity.DTO.ActiveUser;
//import com.lt.test.entity.DTO.ResponseDTO;
//import com.lt.test.consumer.thread.SendCloudingMessageThread;
//import com.lt.test.consumer.util.WebUtils;
//import net.sf.json.JSONObject;
//import org.apache.commons.collections.map.HashedMap;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.subject.Subject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//@RestController
//@RequestMapping("/sys/cloudling")
//public class CloudlingController  {
//
//    private static final Logger log = LoggerFactory.getLogger(CloudlingController.class);
//
//    @Reference(version = "1.0.0")
//    private CloudlingService cloudlingService;
//
////    @Reference(version = "1.0.0")
////    private KtActivitiLeaveService ktActivitiLeaveService;
//
//
//    //提交小云消息
//    @RequestMapping(value = "/submit", method = {RequestMethod.GET, RequestMethod.POST})
//    public Map<String, Object> submit(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        log.info("===============小云开始发送信息=============");
//        Subject subject = SecurityUtils.getSubject();
//        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
//
//        request.setCharacterEncoding("utf-8");
//        Map<String, Object> result = new HashMap<String, Object>();
//        if (request.getParameter("faSongFang") == null) {
//            result.put("success", false);
//            result.put("message", "发送内容过大，请检查图片和内容");
//            return result;
//        }
//        try {
//            Map<String, Object> params = new HashedMap();
//            params.put("model",request.getParameter("model"));
//            params.put("faSongFang",request.getParameter("faSongFang"));
//            params.put("quDaoId",request.getParameter("quDaoId"));
//            params.put("roleId",request.getParameter("roleId"));
//            params.put("phone",request.getParameter("phone"));
//            params.put("cloudId",request.getParameter("cloudId"));
//            params.put("firstUserId",request.getParameter("firstUserId"));
//            params.put("endUserId",request.getParameter("endUserId"));
//
//
//            Map<String, String> user = new HashMap<>();
//            WebUtils.managerStringMap(user);
//            String quDaoId = params.get("quDaoId") == null ? "" : params.get("quDaoId").toString();
//            if (quDaoId.equals("")) {
//                params.put("quDaoId", CustomUtil.getQuDaoId(user.get("fiveChannelCode")));
//            }
//            params.put("fiveChannelCode", user.get("fiveChannelCode"));
//            List<Map<String, Object>> cloudingAndUser = cloudlingService.findUserIdToSendMessage(params);
//
//            Map<String, Object> cloudMessage = new HashMap<>(10);
//            cloudMessage.put("model",request.getParameter("model")); //接收方
//            cloudMessage.put("faSongFang",request.getParameter("faSongFang"));
//
////            if (cloudingAndUser.isEmpty()) {
////                result.put("success", false);
////                result.put("message", "所选择的条件没有对应用户");
////                log.info("-->返回数据  result=" + result);
////                return result;
////            }
//            log.warn(cloudingAndUser.toString());
//
//            String type =  request.getParameter("type");//parameter.get("type").toString();
//            cloudMessage.put("type",type); //发送方式
//            JSONObject obj = new JSONObject();
//            if (type.equals("text")) { //文本信息
//                String content = request.getParameter("content");//parameter.get("content").toString();
//                obj.put("msgContent", content);
//                cloudMessage.put("content",content);
//            } else if (type.equals("image")) {
//                //图片上传
//                String fileStr = request.getParameter("fileStr");//parameter.get("fileStr").toString();
//                String fileType = request.getParameter("fileType");//parameter.get("fileType").toString();
//                // 图片上传
//                if (fileStr != null && fileType != null) {
//
//                    Map<String, String> data = new HashMap<>();
//                    data.put("fileStr", fileStr);
//                    data.put("fileType", fileType);
////                    String url = "http://10.95.0.233:23912/clouderp-upload-rest/api/upload/upload";
////                    String responseResult = HttpClientUtil.sendPostRequest(url, data, null, null);
////                    JSONObject jsonObject = JSONObject.fromObject(responseResult);
////                    if (jsonObject == null || jsonObject.isEmpty() || !"000".equals(jsonObject.get("code"))){
////                        result.put("success", false);
////                        result.put("message", "图片上传失败异常！");
////                        log.error("上传图片失败，错误信息为："+responseResult);
////                        return result;
////                    }
////                    String imgUrl = jsonObject.getString("data");
////                    obj.put("imgUrl",imgUrl);
////                    cloudMessage.put("imgUrl",imgUrl);
////                    System.out.println("====================="+jsonObject);
//                   // obj.put("imgUrl","");
//                }
//                String title1 = request.getParameter("title1");//parameter.get("title1").toString();
//                String title2 = request.getParameter("title2");//parameter.get("title2").toString();
//                String contentText = request.getParameter("text");//parameter.get("text").toString();
//                String linkUrl = SendMessageUtils.generateHtml(contentText);
//                if (null == linkUrl) {
//                    throw new CustomException("生成html文件失败");
//                }
//                obj.put("title", title1);
//                obj.put("summary", title2);
//                obj.put("linkUrl", linkUrl);
//                String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//                obj.put("date", date);
//                cloudMessage.put("title", title1);
//                cloudMessage.put("summary", title2);
//                cloudMessage.put("linkUrl", linkUrl);
//
//                Map parameter = new HashMap();
//                parameter.put("userId", activeUser.getUserId());
//                parameter.put("userName", activeUser.getUsername());
//                parameter.put("leaveDays", title1);
//                parameter.put("leaveReason", linkUrl);
////                ktActivitiLeaveService.create(parameter);
//
//            }
//            String faSongFang = request.getParameter("faSongFang");
//            /*SendMessageUtils.sendMessage(cloudingAndUser, obj, type,faSongFang);
//            cloudMessage.put("date", new Date());
//            Boolean isSuccess = cloudlingService.insertCloudMessageRecord(cloudMessage);
//            if (isSuccess) {
//                log.info("插入小云消息成功");
//            }else {
//                log.error("插入小云消息失败");
//            }*/
//            SendCloudingMessageThread sendCloudingMessageThread = new SendCloudingMessageThread("sendCloudlingMessageThread",cloudlingService,cloudingAndUser,obj, type,faSongFang,cloudMessage);
//            sendCloudingMessageThread.start();
//            result.put("success", true);
//            result.put("message", "消息发送成功！");
//            log.info("-->返回数据  result=" + result);
//        }catch (CustomException e){
//            result.put("success", false);
//            result.put("message", e.getMessage());
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            result.put("success", false);
//            result.put("message", "未知错误，联系技术人员");
//            log.error("-->消息发送异常  exception=" + ex.getMessage());
//        }
//        return result;
//    }
//
//
//
//    /**
//     * @param request
//     * @param response
//     * @throws Exception
//     */
//    @RequestMapping("/findAllChlChannelKind")
//    public Map<String, Object>  findAllChlChannelKind(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        Map<String, Object> result = new HashMap<>(); //作为结果返回
//        result.put("success", false);
//        try {
//            List<Map<String, Object>> chlChannelKinds = cloudlingService.findAllChlChannelKind();
//            result.put("data", chlChannelKinds);
//            result.put("success", true);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    /**
//     * @param request
//     * @param response
//     * @throws Exception
//     */
//    @RequestMapping("/findChlChannel")
//    public  Map<String, Object> findChlChannel(HttpServletRequest request, HttpServletResponse response, Long id, Long pId, Long cck) throws Exception {
//        Map<String, Object> result = new HashMap<>(); //作为结果返回
//        result.put("success", false);
//        try {
//            List<Map<String, Object>> chlChannelKinds = cloudlingService.findChlChannel(id, pId, cck);
//            result.put("data", chlChannelKinds);
//            result.put("success", true);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    /**
//     * @param request
//     * @param response
//     * @throws Exception
//     */
//    @RequestMapping("/findAllChlChannel")
//    public Map<String, Object> findAllChlChannel(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        Map<String, String> params = new HashMap<>();
//        WebUtils.managerStringMap(params);
//
//        Map<String, Object> result = new HashMap<>(); //作为结果返回
//        result.put("success", false);
//        try {
//            List<Map<String, Object>> chlChannelKinds = cloudlingService.findAllChlChannel(params);
//            result.put("data", chlChannelKinds);
//            result.put("success", true);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    /**
//     * 图片上传
//     * @param request
//     * @param response
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "/submitImg", method = {RequestMethod.GET, RequestMethod.POST})
//    public @ResponseBody
//    ResponseDTO submitImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        System.out.println("===============图片上传开始=============");
//        ResponseDTO responseDTO = new ResponseDTO();
//        Map<String, Object> result = new HashMap<String, Object>();
//        JSONObject obj = new JSONObject();
//        //图片上传
//        String fileStr = request.getParameter("fileStr");
//        String fileType = request.getParameter("fileType");
//        String fileStart = request.getParameter("fileStart");
//        // 图片上传
//        if (fileStr != null && fileType != null) {
//            Map<String, String> data = new HashMap<>();
//            data.put("fileStr", fileStr);
//            data.put("fileType", fileType);
////            String url = "http://app1:8080/clouderp-upload-rest/api/upload/upload";
//            String url = "http://clouderp.skystorechain.com:20080/clouderp-upload-rest/api/upload/upload";
//            String responseResult = HttpClientUtil.sendPostRequest(url, data, null, null);
//            JSONObject jsonObject = JSONObject.fromObject(responseResult);
//            if (jsonObject == null || jsonObject.isEmpty() || !"000".equals(jsonObject.get("code"))) {
//                responseDTO.setSuccess(false);
//                responseDTO.setMessage("图片上传失败异常！");
//                System.out.println("上传图片失败，错误信息为：" + responseResult);
//                return responseDTO;
//            }
//            String imgUrl = jsonObject.getString("data");
//            obj.put("imgUrl", imgUrl);
//            responseDTO.setData(imgUrl);
//        }
//        responseDTO.setCode("200");
//        responseDTO.setSuccess(true);
//        responseDTO.setMessage("图片上传成功");
//        return responseDTO;
//    }
//}
//
//
